//
//  NSBezierPath+PXRoundedRectangleAdditions.m
//  Pixen
//
//  Created by Andy Matuschak on 7/3/05.
//  Copyright 2005 Open Sword Group. All rights reserved.
//

#import "NSBezierPath+PXRoundedRectangleAdditions.h"

@implementation NSBezierPath(RoundedRectangle)

+ (NSBezierPath *)bezierPathWithRoundedRect:(NSRect)aRect cornerRadius:(float)radius inCorners:(OSCornerType)corners {
    NSBezierPath *path = [self bezierPath];
    radius = MIN(radius, 0.5f * MIN(NSWidth(aRect), NSHeight(aRect)));
    NSRect rect = NSInsetRect(aRect, radius, radius);
    
    if (corners & OSBottomLeftCorner) {
        [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMinX(rect), NSMinY(rect)) radius:radius startAngle:180.0 endAngle:270.0];
    } else {
        NSPoint cornerPoint = NSMakePoint(NSMinX(aRect), NSMinY(aRect));
        [path appendBezierPathWithPoints:&cornerPoint count:1];
    }
    
    if (corners & OSBottomRightCorner) {
        [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMaxX(rect), NSMinY(rect)) radius:radius startAngle:270.0 endAngle:360.0];
    } else {
        NSPoint cornerPoint = NSMakePoint(NSMaxX(aRect), NSMinY(aRect));
        [path appendBezierPathWithPoints:&cornerPoint count:1];
    }

    if (corners & OSTopRightCorner) {
        [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMaxX(rect), NSMaxY(rect)) radius:radius startAngle:0.0 endAngle:90.0];
    } else {
        NSPoint cornerPoint = NSMakePoint(NSMaxX(aRect), NSMaxY(aRect));
        [path appendBezierPathWithPoints:&cornerPoint count:1];
    }
    
    if (corners & OSTopLeftCorner) {
        [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMinX(rect), NSMaxY(rect)) radius:radius startAngle:90.0 endAngle:180.0];
    } else {
        NSPoint cornerPoint = NSMakePoint(NSMinX(aRect), NSMaxY(aRect));
        [path appendBezierPathWithPoints:&cornerPoint count:1];
    }
    
    [path closePath];
    return path;    
}


+ (NSBezierPath *)bezierPathWithRoundedRect:(NSRect)aRect cornerRadius:(float)radius {
    return [NSBezierPath bezierPathWithRoundedRect:aRect cornerRadius:radius inCorners:OSTopLeftCorner|OSTopRightCorner|OSBottomLeftCorner|OSBottomRightCorner];
}

@end
